﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KTool2.Common;
using KTool2.Database;
using KTool2.Impl;

namespace KTool2.Utility
{
    class KanjiWordProcessor
    {
        public static string GetKanjiInfo(string category, int id, KanjiProperties property)
        {
            if (!DBManagement.IsDBContains(category.Replace('#', '.')))
            {
                throw new Exceptions.DBException(2, category.Replace('#', '.'));
            }

            string info = null;
            StringBuilder cmdTextBuilder = new StringBuilder();

            using (System.Data.OleDb.OleDbConnection connection = KTool2.Database.DBManagement.OpenedConnection)
            //using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(
            //    "SELECT @info FROM @table WHERE ID = @id", connection))
            using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand())
            {
                cmdTextBuilder.Clear();
                cmdTextBuilder.Append("SELECT ").Append(property.ToString()).Append(" FROM [")
                    .Append(category.Replace('#', '.')).Append("] WHERE ID = '#").Append(id).Append("'");
                cmd.CommandText = cmdTextBuilder.ToString();
                //cmd.CommandText = "SELECT @info FROM [" + category.Replace('#', '.') + "] WHERE ID = @id";
                cmd.Connection = connection;
                //cmd.Parameters.AddWithValue("@info", property.ToString());
                //cmd.Parameters.AddWithValue("@id", "#" + id);
                using (System.Data.OleDb.OleDbDataReader reader = DBManagement.ExcuteReader(cmd))
                //using (System.Data.OleDb.OleDbDataReader reader = cmd.ExecuteReader())
                {
                    reader.Read();
                    info = reader.GetString(0).Trim().ToLower();
                }
            }

            GC.WaitForPendingFinalizers();
            GC.Collect();

            return info;
        }

        public static void GetKanji(ref KanjiWord kWord)
        {
            if (!DBManagement.IsDBContains(kWord.Category.Replace('#', '.')))
            {
                throw new Exceptions.DBException(2, kWord.Category.Replace('#', '.'));
            }

            StringBuilder cmdTextBuilder = new StringBuilder();

            using (System.Data.OleDb.OleDbConnection connection = KTool2.Database.DBManagement.OpenedConnection)
            using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand())
            {
                cmdTextBuilder.Clear();
                cmdTextBuilder.Append("SELECT ")
                    .Append(KanjiProperties.ENGLISH)
                    .Append(", ")
                    .Append(KanjiProperties.HANNOM)
                    .Append(", ")
                    .Append(KanjiProperties.HIRAGANA)
                    .Append(", ")
                    .Append(KanjiProperties.VIET)
                    .Append(" FROM [")
                    .Append(kWord.Category.Replace('#', '.')).Append("] WHERE ID = '#").Append(kWord.ID).Append("'");
                cmd.CommandText = cmdTextBuilder.ToString();
                cmd.Connection = connection;
                using (System.Data.OleDb.OleDbDataReader reader = DBManagement.ExcuteReader(cmd))
                {
                    reader.Read();
                    kWord.English = reader.GetString(0).Trim().ToLower();
                    kWord.HanNom = reader.GetString(1).Trim().ToLower();
                    kWord.Hiragana = reader.GetString(2).Trim().ToLower();
                    kWord.Viet = reader.GetString(3).Trim().ToLower();
                }
            }
        }

        public static void SetMemRate(string category, int id, int memRate)
        {
            if (!DBManagement.IsDBContains(category.Replace('#', '.')))
            {
                throw new Exceptions.DBException(2, category.Replace('#', '.'));
            }

            StringBuilder cmdTextBuilder = new StringBuilder();

            using (System.Data.OleDb.OleDbConnection connection = KTool2.Database.DBManagement.OpenedConnection)
            using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand())
            {
                cmdTextBuilder.Append("UPDATE [").Append(category.Replace('#', '.')).Append("] SET [MEMRATE] = ").Append("'#").Append(memRate).Append("' WHERE [ID] = '#").Append(id).Append("'"); ;
                cmd.CommandText = cmdTextBuilder.ToString();
                cmd.Connection = connection;
                DBManagement.ExecuteNonQuery(cmd);
                //cmd.ExecuteNonQuery();
            }
        }
    }
}
